﻿using AssistCore;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace Helper
{
    public class GameAction
    {
        public static Point? FindPropPositon(Bitmap propImage, int matchType, int similar)
        {
            Bitmap bp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            var g = Graphics.FromImage(bp);
            g.CopyFromScreen(0, 0, 0, 0, bp.Size);
            using (MemoryStream msSource = new MemoryStream())
            {
                bp.Save(msSource, ImageFormat.Png);
                using (MemoryStream msTarget = new MemoryStream())
                {
                    propImage.Save(msTarget, ImageFormat.Png);
                    return ImageCore.MatchTemplate(msSource.ToArray(), msTarget.ToArray(), matchType, similar);
                }
            };
        }

        public static void UseProp(CDD dd, Bitmap propImage, int delay, int matchType, int similar)
        {
            var p = GameAction.FindPropPositon(propImage, matchType, similar);
            if (p.HasValue)
            {
                dd.mov(p.Value.X, p.Value.Y);
                Thread.Sleep(100);
                dd.btn(4);
                Thread.Sleep(20);
                dd.btn(8);
                Thread.Sleep(delay);
            }
        }
        public static void OpenBag(CDD dd, int matchType, int similar)
        {
            var p = GameAction.FindPropPositon(Properties.HelpResource.bag_icon, matchType, similar);
            if (p.HasValue)
            {
                Console.WriteLine("背包已打开", p);
                return;
            }
            var p2 = GameAction.FindPropPositon(Properties.HelpResource.bag, matchType, similar);
            if (!p.HasValue)
            {
                Console.WriteLine("找不到背包");
                return;
            }
            dd.mov(p2.Value.X, p2.Value.Y);
            Thread.Sleep(100);
            dd.btn(1);
            dd.btn(2);
        }
    }
}
